home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte1087.arc / MALLETT.ARC / SIEVE.CPP < prev   
Encoding:
Text File  |  1987-09-09  |  3.0 KB  |  124 lines

  1.  
  2. This file contains the two files used for the "sieve" C++ benchmark.
  3. You'll have to separate them.  They are
  4.  
  5.     sv_cpp.h    The include file used to re-implement all the
  6.             arithmetic operators.
  7.     sieve.cpp    The sieve program, slightly modified to:
  8.                a) reference the "sv_cpp.h" file
  9.                b) change the number of loops from 10 to 100.
  10.  
  11.  
  12. -------------------- Beginning of sv_cpp.h --------------------
  13.  
  14. /* SV_CPP.H - new "int" class for the seive benchmark for C++ */
  15.  
  16. class INT {
  17.   int    val;
  18. public:
  19.   INT( int i ) { val = i; }
  20.   INT(){}
  21. /*  ~INT(){} */
  22.   int operator= (int t2);
  23.   int operator+ (int t2);
  24.   int operator- (int t2);
  25.   int operator+= (int t2);
  26.   int operator++ ();
  27.   int operator<= (int t2);
  28.  
  29.   operator int();
  30.  
  31. };
  32.  
  33. /* Include the following statement to prevent inline substitution of
  34.     code implementing arithmetic operations;  enclose it in comments
  35.     to make that code inline.  */
  36. #define    inline
  37.  
  38. /* Likewise, include the following in comments to prevent declaring the
  39.    second term of each operator as a register variable */
  40. /* #define    register */
  41.  
  42. inline int INT :: operator= ( register int t2 )
  43. {
  44. return( val = t2 );
  45. }
  46.  
  47. inline int INT :: operator+ ( register int t2 )
  48. {
  49. return( val + t2 );
  50. }
  51.  
  52. inline int INT :: operator- ( register int t2 )
  53. {
  54. return( val - t2 );
  55. }
  56.  
  57. inline int INT :: operator+= ( register int t2 )
  58. {
  59. return( val += t2 );
  60. }
  61.  
  62. inline int INT :: operator++ ( )
  63. {
  64. return( val++ );
  65. }
  66.  
  67. inline int INT :: operator<= ( register int t2 )
  68. {
  69. return( val <= t2 );
  70. }
  71.  
  72. inline int INT :: operator int ( )
  73. {
  74. return( val );
  75. }
  76.  
  77.  
  78. /* Now for the trick.. */
  79. #define    int    INT
  80.  
  81. -------------------- End of sv_cpp.h --------------------
  82.  
  83.  
  84. -------------------- Beginning of sieve.cpp --------------------
  85.  
  86. /*
  87. Eratosthenes Sieve Prime Number Program in from BYTE January 1983
  88. */
  89.  
  90. #include "sv_cpp.h"
  91.  
  92. #define TRUE     1
  93. #define FALSE    0
  94. #define size  8190
  95.  
  96.      char flags [size + 1];
  97. main()
  98.     {
  99.     int i, prime, k, count, iter;
  100.  
  101.     printf ("100 iterations\n");
  102.     for (iter = 1; iter <= 100; iter++)            /* do program 100 times */
  103.           {
  104.           count = 0;                              /* prime counter */
  105.           for (i = 0; i <= size; i++)             /* set all flags true */
  106.                 flags [i] = TRUE;
  107.           for (i = 0; i <= size; i++)
  108.                {
  109.                     if (flags [i])                /* found a prime */
  110.                     {
  111.                     prime = i + i + 3;            /* twice index + 3 */
  112. /*                  printf ("\n%d", prime);  */
  113.                     for (k = i + prime; k <= size; k+= prime)
  114.                                  flags [k] = FALSE;  /* kill all multiple */
  115.                     count++;                         /* primes found */
  116.                     }
  117.                 }
  118.       }
  119.       printf ("\007%d primes.\n", count);     /* primes found on 100th pass */
  120.  }
  121.  
  122. -------------------- End of sieve.cpp --------------------
  123.  
  124.